<?php
namespace app\gym\controller;
use app\common\controller\AdminBase;
use think\DB;
/*进出日志管理*/
class Behavior extends AdminBase{
	function _initialize() {
			parent::_initialize();	
    }
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function index(){
		$this->assign("now",date("Y-m-d"));
		echo $this->fetch();
	}	
	
	//会员入场
	function member_in(){
		$card_no=input("card_no");
		$shop_id=sp_get_current_shop_id();
		if(!empty($card_no)){
			$interface=db("Interface")->where("card_no='$card_no' and shop_id=$shop_id ")->find();
			$order = db("gym_order")->where(array("card_no" => $card_no, "shop_id" => $shop_id, "is_delete" => 0))->find();
//            $behavior = db("behavior_log")->whereTime('create_time', 'today')->where(array('shop_id' => $shop_id, 'card_no' => $card_no))->order('id desc')->find();
            $today = date("Y-m-d");
            if (strcmp($order['start_date'], $today) > 0 || strcmp($order['end_date'], $today) < 0) {
                $this->ajax_error("该卡合同不在有效期");
            }
            if ($order['order_status'] == 3) {
                $this->ajax_error("此卡未激活");
            } elseif ($order['order_status'] == 4) {
                $this->ajax_error("此卡处于请假状态");
            }
            if ($order['gym_card_type'] == 1) {
                if ($order['able_count'] <= 0) {
                    $this->ajax_error("该卡剩余次数为0");
                } else {
                    db("GymOrder")->where("card_no='$card_no' and shop_id=$shop_id and is_delete=0")->setDec('able_count');
                }
            } elseif ($order['gym_card_type'] == 3) {
                if ($order['able_money'] <= 0) {
                    $this->ajax_error("该卡储值金额为0");
                }
            } elseif ($order['gym_card_type'] == 4) {
                $time_sets = $order['time_sets'];
                if (empty($time_sets)) {
                    $this->ajax_errorr("错峰段未设置");
                } else {
                    $time_sets = explode(",", $time_sets);
                    if(date("H:i") < explode("-",$time_sets[0])[0] || date("H:i") > explode("-",$time_sets[0])[1]) {
                        $this->ajax_error("不在错峰段内不允许入场");
                    }
                    if(date("H:i") < explode("-",$time_sets[1])[0] || date("H:i") > explode("-",$time_sets[1])[1]){
                        $this->ajax_error("不在错峰段内不允许入场");
                    }
                }
            }

            $res = db("BehaviorLog")->insert(
                array(
                    'behavior_type'     => 1,
                    'create_time'       => date('Y-m-d H:i:s'),
                    'behavior_status'   => 1,
                    'card_no'           => $card_no,
                    'in_time'           => date('Y-m-d H:i:s'),
                    'sub_time'          => empty($sub_time) ? null : $sub_time,
                    'name'              => $interface['name'],
                    'mobile'            => $interface['mobile'],
                    'member_id'         => $interface['member_id'],
                    'desc'              => null,
                    'owner_type'        => $interface['owner_type'],
                    'shop_id'           => $interface['shop_id'],
                    'verify_type'       => 2
                )
            );
					
			if($res){
				return	$this->ajax_success("成功");
			}else{
			    return 	$this->ajax_error("入场失败");
			}
		}
		echo $this->fetch();
	}
	//会员出场
	function member_out(){
		$card_no=input("card_no");
		$shop_id=sp_get_current_shop_id();
		if(!empty($card_no)){
			
			$interface=db("Interface")->where("card_no='$card_no' and shop_id=$shop_id ")->find();
            $behavior = db("behavior_log")->whereTime('create_time', 'today')->where(array('shop_id' => $shop_id, 'card_no' => $card_no))->order('id desc')->find();
            if (empty($behavior)) {
                $this->ajax_error("该会员还未签到，请先签到");
            }
            if (empty($behavior['in_time'])) {
                $this->ajax_error("该会员还未签到，请先签到");
            }
			if(empty($interface)){
				return 	$this->ajax_error("此卡号不存在");
			}
            $res = db("BehaviorLog")->where(array("id" => $behavior['id']))->update(
                array(
                    'out_time'           => date('Y-m-d H:i:s'),
                )
            );

            if($res){
                return	$this->ajax_success("成功");
            }else{
                return 	$this->ajax_error("出场失败");
            }
		}
		echo $this->fetch();
	}
	
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10051000");
		$this->assign("menus",$auth_group);
	}

	/**
     * 搜索会员
     */
	public function ajaxSearchMember()
    {
        $shop_id = sp_get_current_shop_id();
        $search = input("search","");
        if (mb_strlen($search) < 2) {
           $this->ajax(array());
        }
        $where = array("o.shop_id" => $shop_id, "o.is_delete" => 0, "m.name|m.mobile|o.card_no" => array("like", "%{$search}%"));
        $list = db("gym_order")->alias("o")
            ->join("m_gym_member m", "o.gym_member_id = m.id", "left")
            ->where($where)
            ->field("o.id, CONCAT(m.name, '--', m.mobile, \"--\", o.card_no) as text, m.face1, o.card_no")
            ->select();
        $this->ajax($list);
    }

    /**
     * 获取卡信息
     */
    public function getCardInfo()
    {
        $shop_id = sp_get_current_shop_id();
        $order_id = input("order_id/d", 0);
        $type = input("type/d", 1); //动作类型 1：入场 2出场
        if (!in_array($type, array(1, 2))) {
            $this->ajax_error("参数错误");
        }
        if (empty($order_id)) {
            $this->ajax_error("参数错误");
        }
        $order = db("gym_order")->where(array("id" => $order_id, "shop_id" => $shop_id, "is_delete" => 0))->find();
        if (empty($order)) {
            $this->ajax_error("找不到该卡号");
        }
        if ($type == 1) {
            $today = date("Y-m-d");

            if ($order['gym_card_type'] == 1) {
                if ($order['able_count'] <= 0) {
                    $this->ajax_error("该卡剩余次数为0");
                }
            } elseif ($order['gym_card_type'] == 3) {
                if ($order['able_money'] <= 0) {
                    $this->ajax_error("该卡储值金额为0");
                }
            } elseif ($order['gym_card_type'] == 4) {
                $time_sets = $order['time_sets'];
                if (empty($time_sets)) {
                    $this->ajax_errorr("错峰段未设置");
                } else {
                    $time_sets = explode(",", $time_sets);
                    if(date("H:i") < explode("-",$time_sets[0])[0] || date("H:i") > explode("-",$time_sets[0])[1]) {
                        $this->ajax_error("不在错峰段内不允许入场");
                    }
                    if(date("H:i") < explode("-",$time_sets[1])[0] || date("H:i") > explode("-",$time_sets[1])[1]){
                        $this->ajax_error("不在错峰段内不允许入场");
                    }
                }
            }
            if (strcmp($order['end_date'], $today) < 0) {
                $this->ajax_error("此卡已过期");
            }
            if ($order['order_status'] == 3) {
                    $this->ajax_error("此卡未激活，是否确定激活?", array("order_id" => $order['id'], 'url' => '/gym/gymMember/activateCard'));
            } elseif ($order['order_status'] == 4) {
                $this->ajax_error("此卡处于请假状态，是否销假?", array("order_id" => $order['id'], 'url' => '/gym/gymMember/member_sick_leave_post'));
            }

            $card_info = db("gym_order")->alias('o')
                ->join('m_gym_member m', 'm.id = o.gym_member_id', 'left')
                ->join('m_gym_employee e', 'e.id = o.member_ship_id', 'left')
                ->join('m_gym_cabinet_order co', 'co.gym_order_id = o.id and co.status = 1', 'left')
                ->join("m_gym_cabinet c", "c.id = co.cabinet_id", "left")
                ->where(array('o.id' => $order_id))
                ->field('m.name as member_name, m.sex, m.mobile,m.face1, m.id_card, o.card_no, e.name as employee_name, m.descs,m.face1,o.gym_card_name,o.start_date as order_start_date,o.end_date as order_end_date,o.able_count,o.able_money,o.gym_card_type,c.cabinet_no, co.start_date as cabinet_start_date, co.end_date as cabinet_end_date')
                ->find();
            $this->ajax_success("获取数据成功", $card_info);
        } elseif ($type == 2) {
            $behavior = db("behavior_log")->whereTime('create_time', 'today')->where(array('shop_id' => $shop_id, 'card_no' => $order['card_no']))->order('id desc')->find();
            if (empty($behavior)) {
                $this->ajax_error("该会员还未签到，请先签到");
            }
            if (empty($behavior['in_time'])) {
                $this->ajax_error("该会员还未签到，请先签到");
            }
            $this->ajax_success("成功");
        }

    }
}
